package com.beidouin.GnssMain.adapter;

/**
 * Created by yuyang on 16/6/24.
 */

import android.content.Context;
import android.database.DataSetObserver;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import java.util.ArrayList;
import java.util.List;

public abstract class UAppendableListAdapter extends BaseAdapter {

    protected final String TAG = this.getClass().getName() + "@" + this.hashCode() + " ";
    protected Context mContext;
    protected List<Object> mData;

    public UAppendableListAdapter() {
        mData = new ArrayList<Object>();
    }

    public UAppendableListAdapter(Context context) {
        this.mContext = context;
        mData = new ArrayList<Object>();
    }

    /**
     * One type is normal data row, the other type is ProgressBar.
     *
     * @return The number of types of Views that will be created by this adapter.
     */
    @Override
    final public int getViewTypeCount() {
        return 1 + getImplViewTypeCount();
    }

    /**
     * The size of the List plus one, the one is the last row, which displays a Progressbar.
     *
     * @return The size of the List plus one for the ProgressBar.
     */
    @Override
    public int getCount() {
        return getImplCount() + (hasBottomLoading() ? 1 : 0);
    }

    @Override
    final public int getItemViewType(int position) {
        return getImplItemViewType(position);
    }


    @Override
    final public Object getItem(int position) {
        return (position >= getImplCount()) ? null : getImplItem(position);
    }

    @Override
    public long getItemId(int position) {
        return (position >= getImplCount()) ? -1 : position;
    }

    protected int getImplItemViewType(int position) {
        return 0;
    }

    protected int getImplViewTypeCount() {
        return 0;
    }

    protected abstract View getImplView(int position, View convertView, ViewGroup parent);

    public int getImplCount() {
        if (mData != null) {
            return mData.size();
        }
        return 0;
    }

    public Object getImplItem(int position) {
        if (mData != null && position < mData.size() && position >= 0) {
            return mData.get(position);
        }
        return null;
    }

    public List getData() {
        return mData;
    }

    public void setData(Object data) {
        if (mData == null) {
            mData = (List) data;
            return;
        }
        if (!mData.equals(data)) {
            mData.clear();
            if (data != null) {
                if (data instanceof List<?>) {
                    for (Object object : (List) data)
                        mData.add(object);
                }
            }
        }
    }

    public void addData(Object data) {
        if (mData == null) {
            mData = (List) data;
            return;
        }
        if (data != null) {
            if (data instanceof List<?>) {
                for (Object object : (List) data)
                    mData.add(object);
            }
        }
    }

    public boolean hasBottomLoading() {
        return false;
    }

    /**
     * Returns the correct view.
     */
    @Override
    public final View getView(int position, View convertView, ViewGroup parent) {
        return getImplView(position, convertView, parent);


    }

    /**
     * add this method to avoid observer is null
     *
     * @param observer
     */
    @Override
    public void unregisterDataSetObserver(DataSetObserver observer) {
        if (observer != null) {
            super.unregisterDataSetObserver(observer);
        }
    }


}


